package com.mdxx.xczd.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.mdxx.xczd.app.ConstantsValue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class PictureUtil {

	public static File toSmallImg(String filePath) {		
		File dir = new File(ConstantsValue.path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		File file = new File(FormatUtil.getSmallFileUrl(filePath));	
		Bitmap bm = getSmallBitmap(filePath);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			bm.compress(Bitmap.CompressFormat.JPEG, 40, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}        
		return file;
	}

	public static Bitmap getSmallBitmap(String filePath) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);
		options.inSampleSize = calculateInSampleSize(options, 320, 240);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(filePath, options);
	}

	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (height > reqHeight || width > reqWidth) {
			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}
}
